﻿#include "GUI.h"
#include "DIALOG.h"
#include "CreateWidget.h"
#include "AppFunction.h"
#include "common_path.h"
#include "password.h"



static uint16_t cipherBufferIndex;
PARA_TCB password_widget[22];


void password_widget_create(WM_HWIN hParent){

	password_widget[0].guiTCB.winType = WIDGET_WINDOW;
	password_widget[0].guiTCB.xPos = 0;
	password_widget[0].guiTCB.yPos = 0;
	password_widget[0].guiTCB.xSize = 720;
	password_widget[0].guiTCB.ySize = 720;
	password_widget[0].appTCB.funType = ENCRYPTION_FUNCTION;
	password_widget[0].appTCB.addrList = 0;
	password_widget[0].guiTCB.id = GetUniqueID();
	password_widget[0].pPreviousLayer = NULL;
	password_widget[0].pNextLayer = NULL;
	password_widget[0].parent = 0;
	password_widget[0].winLayer = 2;
	password_widget[0].transparency = 0;
	SetPictureHmem(&password_widget[0], 0, 0, GetPicturePathHmem(I_DTA_PATH_BGHOME));
	password_widget[0].guiTCB.specialInfo.occupy[0] = 5555;
	password_widget[0].guiTCB.specialInfo.encryption.cipherPreperiod = 60;


	password_widget[1].guiTCB.winType = WIDGET_BUTTON;
	password_widget[1].guiTCB.xPos = 0;
	password_widget[1].guiTCB.yPos = 20;
	password_widget[1].guiTCB.xSize = 120;
	password_widget[1].guiTCB.ySize = 120;
	password_widget[1].appTCB.funType = RETURN_FUNCTION;
	password_widget[1].appTCB.addrList = 0;
	password_widget[1].guiTCB.id = GetUniqueID();
	password_widget[1].pPreviousLayer = NULL;
	password_widget[1].pNextLayer = NULL;
	password_widget[1].parent = NULL;
	sprintf(password_widget[1].guiTCB.onPath, "%s", RETURN);
	sprintf(password_widget[1].guiTCB.offPath, "%s", RETURN);


	password_widget[2].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[2].guiTCB.winName, "请输入密码");
	password_widget[2].guiTCB.xPos = 260;
	password_widget[2].guiTCB.yPos = 80;
	password_widget[2].guiTCB.xSize = 160;
	password_widget[2].guiTCB.ySize = 50;
	password_widget[2].appTCB.funType = SWITCH_ACTUATOR;
	password_widget[2].appTCB.addrList = 0;
	password_widget[2].guiTCB.id = GetUniqueID();
	password_widget[2].pPreviousLayer = NULL;
	password_widget[2].pNextLayer = NULL;
	password_widget[2].parent = NULL;




	password_widget[3].guiTCB.winType = WIDGET_EDIT;
	AddStringToBuffer(password_widget[3].guiTCB.winName, "文本框");
	password_widget[3].guiTCB.xPos = 260;
	password_widget[3].guiTCB.yPos = 125;
	password_widget[3].guiTCB.xSize = 160;
	password_widget[3].guiTCB.ySize = 48;
	password_widget[3].appTCB.funType = CIPHER_SHOW;
	password_widget[3].appTCB.addrList = 0;
	password_widget[3].guiTCB.id = GetUniqueID();
	password_widget[3].pPreviousLayer = NULL;
	password_widget[3].pNextLayer = NULL;
	password_widget[3].parent = NULL;
	password_widget[3].guiTCB.pFont = &GUI_FONT_48;
	password_widget[3].guiTCB.align = GUI_TA_CENTER | GUI_TA_VCENTER | 0x80;


	password_widget[4].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[4].guiTCB.winName, "1");
	password_widget[4].guiTCB.xPos = 60;
	password_widget[4].guiTCB.yPos = 250;
	password_widget[4].guiTCB.xSize = 160;
	password_widget[4].guiTCB.ySize = 80;
	password_widget[4].appTCB.funType = CIPHER_NUMBER1;
	password_widget[4].appTCB.addrList = 0;
	password_widget[4].guiTCB.id = GetUniqueID();
	password_widget[4].guiTCB.pFont = &GUI_FONT_64;
	password_widget[4].pPreviousLayer = NULL;
	password_widget[4].pNextLayer = NULL;
	password_widget[4].parent = NULL;
	



	password_widget[5].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[5].guiTCB.winName, "2");
	password_widget[5].guiTCB.xPos = 260;
	password_widget[5].guiTCB.yPos = 250;
	password_widget[5].guiTCB.xSize = 160;
	password_widget[5].guiTCB.ySize = 80;
	password_widget[5].appTCB.funType = CIPHER_NUMBER2;
	password_widget[5].appTCB.addrList = 0;
	password_widget[5].guiTCB.id = GetUniqueID();
	password_widget[5].pPreviousLayer = NULL;
	password_widget[5].pNextLayer = NULL;
	password_widget[5].parent = NULL;
	password_widget[5].guiTCB.pFont = &GUI_FONT_64;



	password_widget[6].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[6].guiTCB.winName, "3");
	password_widget[6].guiTCB.xPos = 460;
	password_widget[6].guiTCB.yPos = 250;
	password_widget[6].guiTCB.xSize = 160;
	password_widget[6].guiTCB.ySize = 80;
	password_widget[6].appTCB.funType = CIPHER_NUMBER3;
	password_widget[6].appTCB.addrList = 0;
	password_widget[6].guiTCB.id = GetUniqueID();
	password_widget[6].pPreviousLayer = NULL;
	password_widget[6].pNextLayer = NULL;
	password_widget[6].parent = NULL;
	password_widget[6].guiTCB.pFont = &GUI_FONT_64;


	password_widget[7].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[7].guiTCB.winName, "4");
	password_widget[7].guiTCB.xPos = 60;
	password_widget[7].guiTCB.yPos = 350;
	password_widget[7].guiTCB.xSize = 160;
	password_widget[7].guiTCB.ySize = 80;
	password_widget[7].appTCB.funType = CIPHER_NUMBER4;
	password_widget[7].appTCB.addrList = 0;
	password_widget[7].guiTCB.id = GetUniqueID();
	password_widget[7].pPreviousLayer = NULL;
	password_widget[7].pNextLayer = NULL;
	password_widget[7].parent = NULL;
	password_widget[7].guiTCB.pFont = &GUI_FONT_64;


	password_widget[8].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[8].guiTCB.winName, "5");
	password_widget[8].guiTCB.xPos = 260;
	password_widget[8].guiTCB.yPos = 350;
	password_widget[8].guiTCB.xSize = 160;
	password_widget[8].guiTCB.ySize = 80;
	password_widget[8].appTCB.funType = CIPHER_NUMBER5;
	password_widget[8].appTCB.addrList = 0;
	password_widget[8].guiTCB.id = GetUniqueID();
	password_widget[8].pPreviousLayer = NULL;
	password_widget[8].pNextLayer = NULL;
	password_widget[8].parent = NULL;
	password_widget[8].guiTCB.pFont = &GUI_FONT_64;


	password_widget[9].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[9].guiTCB.winName, "6");
	password_widget[9].guiTCB.xPos = 460;
	password_widget[9].guiTCB.yPos = 350;
	password_widget[9].guiTCB.xSize = 160;
	password_widget[9].guiTCB.ySize = 80;
	password_widget[9].appTCB.funType = CIPHER_NUMBER6;
	password_widget[9].appTCB.addrList = 0;
	password_widget[9].guiTCB.id = GetUniqueID();
	password_widget[9].pPreviousLayer = NULL;
	password_widget[9].pNextLayer = NULL;
	password_widget[9].parent = NULL;
	password_widget[9].guiTCB.pFont = &GUI_FONT_64;



	password_widget[10].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[10].guiTCB.winName, "7");
	password_widget[10].guiTCB.xPos = 60;
	password_widget[10].guiTCB.yPos = 450;
	password_widget[10].guiTCB.xSize = 160;
	password_widget[10].guiTCB.ySize = 80;
	password_widget[10].appTCB.funType = CIPHER_NUMBER7;
	password_widget[10].appTCB.addrList = 0;
	password_widget[10].guiTCB.id = GetUniqueID();
	password_widget[10].pPreviousLayer = NULL;
	password_widget[10].pNextLayer = NULL;
	password_widget[10].parent = NULL;
	password_widget[10].guiTCB.pFont = &GUI_FONT_64;



	password_widget[11].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[11].guiTCB.winName, "8");
	password_widget[11].guiTCB.xPos = 260;
	password_widget[11].guiTCB.yPos = 450;
	password_widget[11].guiTCB.xSize = 160;
	password_widget[11].guiTCB.ySize = 80;
	password_widget[11].appTCB.funType = CIPHER_NUMBER8;
	password_widget[11].appTCB.addrList = 0;
	password_widget[11].guiTCB.id = GetUniqueID();
	password_widget[11].pPreviousLayer = NULL;
	password_widget[11].pNextLayer = NULL;
	password_widget[11].parent = NULL;
	password_widget[11].guiTCB.pFont = &GUI_FONT_64;


	password_widget[12].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[12].guiTCB.winName, "9");
	password_widget[12].guiTCB.xPos = 460;
	password_widget[12].guiTCB.yPos = 450;
	password_widget[12].guiTCB.xSize = 160;
	password_widget[12].guiTCB.ySize = 80;
	password_widget[12].appTCB.funType = CIPHER_NUMBER9;
	password_widget[12].appTCB.addrList = 0;
	password_widget[12].guiTCB.id = GetUniqueID();
	password_widget[12].pPreviousLayer = NULL;
	password_widget[12].pNextLayer = NULL;
	password_widget[12].parent = NULL;
	password_widget[12].guiTCB.pFont = &GUI_FONT_64;


	password_widget[13].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[13].guiTCB.winName, "0");
	password_widget[13].guiTCB.xPos = 260;
	password_widget[13].guiTCB.yPos = 550;
	password_widget[13].guiTCB.xSize = 160;
	password_widget[13].guiTCB.ySize = 80;
	password_widget[13].appTCB.funType = CIPHER_NUMBER0;
	password_widget[13].appTCB.addrList = 0;
	password_widget[13].guiTCB.id = GetUniqueID();
	password_widget[13].pPreviousLayer = NULL;
	password_widget[13].pNextLayer = NULL;
	password_widget[13].parent = NULL;
	password_widget[13].guiTCB.pFont = &GUI_FONT_64;



	password_widget[14].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[14].guiTCB.winName, "确认");
	password_widget[14].guiTCB.xPos = 60;
	password_widget[14].guiTCB.yPos = 550;
	password_widget[14].guiTCB.xSize = 160;
	password_widget[14].guiTCB.ySize = 80;
	password_widget[14].appTCB.funType = CIPHER_CONFIRM | JUMP_TO_WINDOW;
	password_widget[14].appTCB.addrList = 0;
	password_widget[14].guiTCB.id = GetUniqueID();
	password_widget[14].pPreviousLayer = NULL;
	password_widget[14].pNextLayer = NULL;
	password_widget[14].parent = NULL;
	//password_widget[14].guiTCB.pFont = &GUI_FontFont64;


	password_widget[15].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[15].guiTCB.winName, "删除");
	password_widget[15].guiTCB.xPos = 460;
	password_widget[15].guiTCB.yPos = 550;
	password_widget[15].guiTCB.xSize = 160;
	password_widget[15].guiTCB.ySize = 80;
	password_widget[15].appTCB.funType = CIPHER_CANCEL;
	password_widget[15].appTCB.addrList = 0;
	password_widget[15].guiTCB.id = GetUniqueID();
	password_widget[15].pPreviousLayer = NULL;
	password_widget[15].pNextLayer = NULL;
	password_widget[15].parent = NULL;
	//password_widget[15].guiTCB.pFont = &GUI_FontFont64;




	password_widget[16].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[16].guiTCB.winName, "请输入新密码：   ");
	password_widget[16].guiTCB.xPos = 130;
	password_widget[16].guiTCB.yPos = 130;
	password_widget[16].guiTCB.xSize = 260;
	password_widget[16].guiTCB.ySize = 50;
	password_widget[16].appTCB.funType = CIPHER_SHOW;
	password_widget[16].appTCB.addrList = 0;
	password_widget[16].guiTCB.id = GetUniqueID();
	password_widget[16].pPreviousLayer = NULL;
	password_widget[16].pNextLayer = NULL;
	password_widget[16].parent = NULL;
	password_widget[16].guiTCB.align = GUI_TA_LEFT | GUI_TA_VCENTER | 0x80;


	password_widget[17].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[17].guiTCB.winName, "请输入上锁时间（秒）：");
	password_widget[17].guiTCB.xPos = 130;
	password_widget[17].guiTCB.yPos = 180;
	password_widget[17].guiTCB.xSize = 270;
	password_widget[17].guiTCB.ySize = 50;
	password_widget[17].appTCB.funType = CIPHER_SHOW;
	password_widget[17].appTCB.addrList = 0;
	password_widget[17].guiTCB.id = GetUniqueID();
	password_widget[17].pPreviousLayer = NULL;
	password_widget[17].pNextLayer = NULL;
	password_widget[17].parent = NULL;
	password_widget[17].guiTCB.align = GUI_TA_LEFT | GUI_TA_VCENTER | 0x80;


	password_widget[18].guiTCB.winType = WIDGET_EDIT;
	//AddStringToBuffer(password_widget[18].guiTCB.winName, "新密码");
	password_widget[18].guiTCB.xPos = 410;
	password_widget[18].guiTCB.yPos = 125;
	password_widget[18].guiTCB.xSize = 160;
	password_widget[18].guiTCB.ySize = 48;
	password_widget[18].appTCB.funType = CIPHER_NEW;
	password_widget[18].appTCB.addrList = 0;
	password_widget[18].guiTCB.id = GetUniqueID();
	password_widget[18].pPreviousLayer = NULL;
	password_widget[18].pNextLayer = NULL;
	password_widget[18].parent = NULL;
	password_widget[18].guiTCB.pFont = &GUI_FONT_48;



	password_widget[19].guiTCB.winType = WIDGET_EDIT;
	//AddStringToBuffer(password_widget[19].guiTCB.winName, "解锁时间(秒)");
	password_widget[19].guiTCB.xPos = 410;
	password_widget[19].guiTCB.yPos = 175;
	password_widget[19].guiTCB.xSize = 160;
	password_widget[19].guiTCB.ySize = 48;
	password_widget[19].appTCB.funType = CIPHER_PERIOD;
	password_widget[19].appTCB.addrList = 0;
	password_widget[19].guiTCB.id = GetUniqueID();
	password_widget[19].pPreviousLayer = NULL;
	password_widget[19].pNextLayer = NULL;
	password_widget[19].parent = NULL;
	password_widget[19].guiTCB.pFont = &GUI_FONT_48;



	password_widget[20].guiTCB.winType = WIDGET_STATIC_TEXT;
	AddStringToBuffer(password_widget[20].guiTCB.winName, "请输入当前密码：");
	password_widget[20].guiTCB.xPos = 130;
	password_widget[20].guiTCB.yPos = 80;
	password_widget[20].guiTCB.xSize = 260;
	password_widget[20].guiTCB.ySize = 50;
	password_widget[20].appTCB.funType = CIPHER_OLD;
	password_widget[20].appTCB.addrList = 0;
	password_widget[20].guiTCB.id = GetUniqueID();
	password_widget[20].pPreviousLayer = NULL;
	password_widget[20].pNextLayer = NULL;
	password_widget[20].parent = NULL;
	password_widget[20].guiTCB.align = GUI_TA_LEFT | GUI_TA_VCENTER | 0x80;



	password_widget[21].guiTCB.winType = WIDGET_EDIT;
	AddStringToBuffer(password_widget[21].guiTCB.winName, "旧密码");
	password_widget[21].guiTCB.xPos = 410;
	password_widget[21].guiTCB.yPos = 75;
	password_widget[21].guiTCB.xSize = 160;
	password_widget[21].guiTCB.ySize = 48;
	password_widget[21].appTCB.funType = CIPHER_OLD;
	password_widget[21].appTCB.addrList = 0;
	password_widget[21].guiTCB.id = GetUniqueID();
	password_widget[21].pPreviousLayer = NULL;
	password_widget[21].pNextLayer = NULL;
	password_widget[21].parent = NULL;
	password_widget[21].guiTCB.pFont = &GUI_FONT_48;



	Concatenate(password_widget, 22, 2);

	SetEncryptionPage(password_widget);

	CreateWidget(&password_widget[0], hParent, 0);

}


void SetModifyPassword(uint8_t enable)
{
	GUI_HWIN hWin, hParent;


	password_widget[0].guiTCB.specialInfo.encryption.cipherModify = enable;
	hParent                                                       = password_widget[2].parent;

	if (enable == 1)
	{
		hWin = WM_GetDialogItem(hParent, password_widget[16].guiTCB.id);
		WM_ShowWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[17].guiTCB.id);
		WM_ShowWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[18].guiTCB.id);
		password_widget[18].guiTCB.winName[0] = 0;
		EDIT_SetText(hWin, password_widget[18].guiTCB.winName);
		WM_ShowWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[19].guiTCB.id);
		password_widget[19].guiTCB.winName[0] = 0;
		EDIT_SetText(hWin, password_widget[19].guiTCB.winName);
		WM_ShowWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[20].guiTCB.id);
		WM_ShowWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[21].guiTCB.id);
		password_widget[21].guiTCB.winName[0] = 0;
		EDIT_SetText(hWin, password_widget[21].guiTCB.winName);
		WM_ShowWindow(hWin);



		hWin = WM_GetDialogItem(hParent, password_widget[2].guiTCB.id);
		WM_HideWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[3].guiTCB.id);
		WM_HideWindow(hWin);

		cipherBufferIndex = 21;
	}
	else
	{
		hWin = WM_GetDialogItem(hParent, password_widget[16].guiTCB.id);
		WM_HideWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[17].guiTCB.id);
		WM_HideWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[18].guiTCB.id);
		WM_HideWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[19].guiTCB.id);
		WM_HideWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[20].guiTCB.id);
		WM_HideWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[21].guiTCB.id);
		WM_HideWindow(hWin);



		hWin = WM_GetDialogItem(hParent, password_widget[2].guiTCB.id);
		WM_ShowWindow(hWin);

		hWin = WM_GetDialogItem(hParent, password_widget[3].guiTCB.id);
		password_widget[3].guiTCB.winName[0] = 0;
		EDIT_SetText(hWin, password_widget[3].guiTCB.winName);
		WM_ShowWindow(hWin);

		cipherBufferIndex = 3;
	}
}


void ModifyCipher(pPARA_TCB pBase, pPARA_TCB pPara)
{
	pPARA_TCB pTcb;
	uint32_t length, oldCipher, newCipher, cipherPeriod;
	GUI_HWIN hWin;


	oldCipher                      = atoi(password_widget[21].guiTCB.winName);
	newCipher                      = atoi(password_widget[18].guiTCB.winName);
	cipherPeriod                   = atoi(password_widget[19].guiTCB.winName);
	pPara                          = pBase->pNextLayer;
	password_widget[14].pNextLayer = NULL;

	if (newCipher == 0 || newCipher >= 1000 && newCipher < 10000)
	{
		hWin = WM_GetDialogItem(pBase->parent, password_widget[18].guiTCB.id);
		EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_BLACK);

		if (cipherPeriod < 4096)
		{
			hWin = WM_GetDialogItem(pBase->parent, password_widget[19].guiTCB.id);
			EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_BLACK);

			pTcb = pPara;
			if (oldCipher == password_widget[0].guiTCB.specialInfo.occupy[0])                     // 等于全局密码
				pPara = password_widget;
			else if (oldCipher != pPara->guiTCB.specialInfo.occupy[0])                            // 不等于独立密码
			{
				password_widget[14].pNextLayer = NULL;
				pPara->guiTCB.specialInfo.encryption.cipherOK = 0;
				hWin = WM_GetDialogItem(pBase->parent, password_widget[21].guiTCB.id);
				EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_RED);
				return;
			}

			if (newCipher > 0)
			{
				if (newCipher == 9999)
				{
					newCipher    = 0;
					cipherPeriod = 0;
				}

				pTcb->guiTCB.specialInfo.occupy[0] = newCipher;
				pTcb->guiTCB.specialInfo.encryption.cipherPreperiod = cipherPeriod;
			}
			/* 此处应该将密码保存到硬盘里面， 以便复位后能读取新值。   */
			// 此处调用存档函数


			if (pPara->guiTCB.specialInfo.encryption.cipherTimer == 0)
			{
				length = pPara->guiTCB.specialInfo.encryption.cipherPreperiod;
				if (length == 0)
					length = CIPHER_TIMER_PERIOD;
				else
					length *= 1000;

				pPara->guiTCB.specialInfo.encryption.cipherTimer = WM_CreateTimer(pPara->parent, CIPHER_TIMER_ID, length, 0);
			}
			else
				WM_RestartTimer(pPara->guiTCB.specialInfo.encryption.cipherTimer, 0);

			pPara->guiTCB.specialInfo.encryption.cipherOK = 1;
			password_widget[14].pNextLayer = password_widget[0].pNextLayer;
			
			password_widget[21].guiTCB.winName[0] = 0;
			hWin = WM_GetDialogItem(pBase->parent, password_widget[21].guiTCB.id);
			EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_BLACK);
			EDIT_SetText(hWin, password_widget[21].guiTCB.winName);

			password_widget[18].guiTCB.winName[0] = 0;
			hWin = WM_GetDialogItem(pBase->parent, password_widget[18].guiTCB.id);
			EDIT_SetText(hWin, password_widget[18].guiTCB.winName);

			password_widget[19].guiTCB.winName[0] = 0;
			hWin = WM_GetDialogItem(pBase->parent, password_widget[19].guiTCB.id);
			EDIT_SetText(hWin, password_widget[19].guiTCB.winName);
		}
		else                                                                                      // 时间无效
		{
			hWin = WM_GetDialogItem(pBase->parent, password_widget[19].guiTCB.id);
			EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_RED);
		}
	}
	else                                                                                          // 新密码无效
	{
		hWin = WM_GetDialogItem(pBase->parent, password_widget[18].guiTCB.id);
		EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_RED);
	}
}


void PasswordFunction(pPARA_TCB pBase, pPARA_TCB pPara, uint8_t stage)
{
	GUI_HWIN hWin;
	uint32_t length, cipher;


	if (stage == 0)                                // 初始化
	{
		if (pPara == &password_widget[3])
		{
			SetModifyPassword(0);
		}
	}
	else if (stage == 1)                           // 应用功能
	{
		if (pPara->appTCB.keyMsg == WM_NOTIFICATION_RELEASED)
		{
#if defined(DEBUG_IN_PC)
			EDIT_GetText(WM_GetDialogItem(pPara->parent, password_widget[cipherBufferIndex].guiTCB.id), password_widget[cipherBufferIndex].guiTCB.winName, 100);
#endif

			length = strlen(password_widget[cipherBufferIndex].guiTCB.winName);
			hWin = WM_GetDialogItem(pPara->parent, password_widget[cipherBufferIndex].guiTCB.id);

			switch ((pPara->appTCB.funType & NORMAL_MASK))
			{
			case CIPHER_NUMBER0:
			case CIPHER_NUMBER1:
			case CIPHER_NUMBER2:
			case CIPHER_NUMBER3:
			case CIPHER_NUMBER4:
			case CIPHER_NUMBER5:
			case CIPHER_NUMBER6:
			case CIPHER_NUMBER7:
			case CIPHER_NUMBER8:
			case CIPHER_NUMBER9:
				//if (length <= 4)
				{
					AddStringToBuffer(password_widget[cipherBufferIndex].guiTCB.winName, pPara->guiTCB.winName);
					EDIT_SetText(hWin, password_widget[cipherBufferIndex].guiTCB.winName);
				}

				break;
			case CIPHER_OLD:
				cipherBufferIndex = 21;
				break;
			case CIPHER_NEW:
				cipherBufferIndex = 18;
				break;
			case CIPHER_PERIOD:
				cipherBufferIndex = 19;
				break;
			case CIPHER_CONFIRM:
				if (password_widget[0].guiTCB.specialInfo.encryption.cipherModify == 1)
					ModifyCipher(pBase, pPara);
				else
				{
					length = atoi(password_widget[3].guiTCB.winName);
					pPara = pBase->pNextLayer;

					if (length >= 1000 && length < 9999)
					{
						if (length == password_widget[0].guiTCB.specialInfo.occupy[0])                      // 等于全局密码
							pPara = password_widget;
						else if (length != pPara->guiTCB.specialInfo.occupy[0])                              // 等于独立密码
						{
							password_widget[14].pNextLayer = NULL;
							pPara->guiTCB.specialInfo.encryption.cipherOK = 0;
							EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_RED);
							break;
						}

						if (pPara->guiTCB.specialInfo.encryption.cipherTimer == 0)
						{
							length = pPara->guiTCB.specialInfo.encryption.cipherPreperiod;
							if (length == 0)
								length = CIPHER_TIMER_PERIOD;
							else
								length *= 1000;

							pPara->guiTCB.specialInfo.encryption.cipherTimer = WM_CreateTimer(pPara->parent, CIPHER_TIMER_ID, length, 0);
						}
						else
							WM_RestartTimer(pPara->guiTCB.specialInfo.encryption.cipherTimer, 0);

						pPara->guiTCB.specialInfo.encryption.cipherOK = 1;
						password_widget[14].pNextLayer = password_widget[0].pNextLayer;
						password_widget[3].guiTCB.winName[0] = 0;
						EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_BLACK);
						EDIT_SetText(hWin, password_widget[3].guiTCB.winName);
					}
					else
					{
						password_widget[14].pNextLayer = NULL;
						pPara->guiTCB.specialInfo.encryption.cipherOK = 0;
						EDIT_SetTextColor(hWin, EDIT_CI_ENABLED, GUI_RED);
					}
				}

				break;
			case CIPHER_CANCEL:
				if (length > 0)
				{
					password_widget[cipherBufferIndex].guiTCB.winName[--length] = 0;
					EDIT_SetText(hWin, password_widget[cipherBufferIndex].guiTCB.winName);
				}

				break;
			default:
				break;
			}
		}
	}
	else if (stage == 2)                           // 定时器功能
	{
		pBase->guiTCB.specialInfo.encryption.cipherOK = 0;
	}
}